package message;

import model.RoomInfo;
import socket.ChessServer;

/**
 * 是否同意对方悔棋.
 *
 * @author 张紘溢
 */
public class BackResultMessage extends BaseMessage {
    private boolean agree;
    private int roomId;
    private boolean isLeft;

    public BackResultMessage(boolean agree, int roomId, boolean isLeft) {
        this.agree = agree;
        this.roomId = roomId;
        this.isLeft = isLeft;
    }

    @Override
    public void sendToServer() {
        RoomInfo roomInfo = ChessServer.getInstance().getRooms().get(roomId);
        if (agree) {//如果同意
            if (isLeft) {
                //向对方发送报文,更改对方棋盘
                ServerBackSucceed serverBackSucceed = new ServerBackSucceed();
                ChessServer.getInstance().sendMsgToClient(serverBackSucceed, roomInfo.getRightPlayer());
                //向自己发送报文,更改自己棋盘
                ServerBackSucceedSelfMessage serverBackSucceedSelfMessage = new ServerBackSucceedSelfMessage();
                ChessServer.getInstance().sendMsgToClient(serverBackSucceedSelfMessage, roomInfo.getLeftPlayer());
            } else {
                //向对方发送报文,更改对方棋盘
                ServerBackSucceed serverBackSucceed = new ServerBackSucceed();
                ChessServer.getInstance().sendMsgToClient(serverBackSucceed, roomInfo.getLeftPlayer());
                //向自己发送报文,更改自己棋盘
                ServerBackSucceedSelfMessage serverBackSucceedSelfMessage = new ServerBackSucceedSelfMessage();
                ChessServer.getInstance().sendMsgToClient(serverBackSucceedSelfMessage, roomInfo.getRightPlayer());
            }
        } else {
            //不用更改棋盘
            if (isLeft) {
                ServerBackFail serverBackFail = new ServerBackFail();
                ChessServer.getInstance().sendMsgToClient(serverBackFail, roomInfo.getRightPlayer());
            } else {
                ServerBackFail serverBackFail = new ServerBackFail();
                ChessServer.getInstance().sendMsgToClient(serverBackFail, roomInfo.getLeftPlayer());
            }
        }
    }

    @Override
    public void selfExecute() {

    }
}
